1027A - Palindromic Twist - CodeForces Solution


implementation strings *1000

Please click on ads to support us..

Python Code:


import bisect
from sys import stdin

input = stdin.readline

mod = 10 ** 9 + 7
eps = 10 ** -9


def linp(type=int):
    return list(map(type, input().split()))


def minp(type=int):
    return map(type, input().split())


def tinp(type=int):
    return type(input())


def __gcd(a, b):
    return a if b == 0 else __gcd(b, a % b)


def __lcm(a, b):
    return a * b / __gcd(a, b)


def __fact(n):
    return 1 if n == 1 else n * __fact(n - 1)


def __mex(a):
    mex = 0
    a.sort()
    for x in a:
        if x <= mex:
            mex += 1
        else:
            break
    return mex


def __dist(x1, y1, x2, y2):
    return (x1 - x2) ** 2 + (y1 - y2) ** 2


def __getprimes(n):
    isprime = [True for i in range(n + 1)]
    primes = []

    for i in range(2, n + 1):
        if isprime[i]:
            for j in range(i * i, n + 1, i):
                isprime[j] = False
    for i in range(2, n + 1):
        if isprime[i]:
            primes.append(i)
    return primes


def __getdividers(n):
    i = 1
    ret = []
    while i * i <= n:
        if n % i == 0:
            ret.append(i)

            if i * i != n:
                ret.append(n // i)
        i += 1
    ret.sort()
    return ret


def __modInverse(a, m):
	m0 = m
	y = 0
	x = 1
	if (m == 1):
		return 0
	
	while (a > 1):
		q = a // m
		t = m
		m = a % m
		a = t
		t = y
		y = x - q * y
		x = t
	if (x < 0):
		x = x + m0
	return x

def __isprime(n):
    if n < 2:
        return False
    i = 2
    while i * i <= n:
        if n % i == 0:
            return False
        i += 1
    return True


def __cntprimediv(n):
    ret = 0
    x = n
    i = 2
    while i * i <= x:
        while n % i == 0:
            n //= i
            ret += 1
        i += 1
    if n > 1:
        ret += 1
    return ret


def __primefactors(n):
    ret = []
    x = n
    i = 2
    while i * i <= x:
        while n % i == 0:
            ret.append(i)
            n //= i
        i += 1
    if n > 1:
        ret.append(n)
    return ret


def __sumdigit(n):
    ret = 0
    while n > 0:
        ret += n % 10
        n //= 10
    return ret


def __zfunc(s):
    n = len(s)
    z = [0 for i in range(n)]
    l = 0
    r = 0
    for i in range(1, n):
        if r >= i:
            z[i] = min(z[i - l], r - i + 1)
        while z[i] + i < n and s[z[i]] == s[z[i] + i]:
            z[i] += 1
        if i + z[i] - 1 > r:
            l = i
            r = i + z[i] - 1
    return z


def __to(n, x):
    ret = ''
    while n > 0:
        q = n % x
        if q < 10:
            ret += str(q)
        else:
            ret += chr(q - 10 + ord('a'))
        n //= x
    return ret[::-1]


def solve(t):
	n = tinp()
	s = input().strip()
	ok = True
	for i in range(n // 2):
		if s[i] != s[n - i - 1] and abs(ord(s[i]) - ord(s[n - i - 1])) != 2:
			ok = False
	print('YES' if ok else 'NO')

t = 1
t = int(input())

for i in range(t):
    solve(i + 1)

C++ Code:

#include<iostream>
using namespace std;
int main(){
    int t;
    cin>>t;
    while(t--){
    int n;
    cin>>n;
    string s;
    cin>>s;
     
    bool flag=true;
    int j=n-1;
    for(int i=0;i<n/2;i++,j--){
        if(s[i]=='a'){
            if(s[j]!='a' && s[j]!='b' && s[j]!='c'){
                {
                    flag=false;
                    break;
                }
            }

            if(s[i]=='z'){
            if(s[j]!='z' && s[j]!='y' && s[j]!='x'){
                {
                    flag=false;
                    break;
                }
            }
        }

    }

    int ans=abs(s[i]-s[j]);
    if(ans>2 || ans==1 ){
        flag=false;
        break;
    }
   
   }
   if(flag)
   cout<<"YES"<<endl;
   else
   cout<<"NO"<<endl;
}
return 0;
}



Comments

Submit
0 Comments
More Questions

706B - Interesting drink
1265A - Beautiful String
214A - System of Equations
287A - IQ Test
1108A - Two distinct points
1064A - Make a triangle
1245C - Constanze's Machine
1005A - Tanya and Stairways
1663F - In Every Generation
1108B - Divisors of Two Integers
1175A - From Hero to Zero
1141A - Game 23
1401B - Ternary Sequence
598A - Tricky Sum
519A - A and B and Chess
725B - Food on the Plane
154B - Colliders
127B - Canvas Frames
107B - Basketball Team
245A - System Administrator
698A - Vacations
1216B - Shooting
368B - Sereja and Suffixes
1665C - Tree Infection
1665D - GCD Guess
29A - Spit Problem
1097B - Petr and a Combination Lock
92A - Chips
1665B - Array Cloning Technique
1665A - GCD vs LCM